package com.dhp.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhp.entity.User;
import com.dhp.mapper.UserMapper;
import com.dhp.security.entity.MyUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: MyUserDetailsImpl
 * @Description: TODO
 * @Author: dhp
 * @Date: 2020/12/6 13:37
 * @Version: v1.0
 */
@Service("userDetailsService")
public class MyUserDetailsImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //先找到对应的账号密码
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",s);
        User user = userMapper.selectOne(userQueryWrapper);
        if(null == user){
            return null;
        }
        String username = user.getUsername();
        String password = user.getPassword();
        //找到该账号下面的权限
        List<String> permissionValueList = userMapper.getPermissionValueList(user.getId());
        MyUserDetails myUserDetails = new MyUserDetails(username, password, permissionValueList);
        return myUserDetails;
    }
}
